package com.ruihui.activity.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ruihui.activity.exceprion.GetException;
import com.ruihui.activity.pojo.Acgoods;
import com.ruihui.activity.pojo.Acpgift;
import com.ruihui.activity.service.AcgoodsService;
import com.ruihui.activity.service.AcpgiftService;
import com.ruihui.activity.util.AcgoodsType;
import com.ruihui.activity.util.ConstantHolder;
import com.ruihui.activity.util.DateUtil;
import com.ruihui.activity.util.Result;
import com.ruihui.activity.util.ResultCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 活动赠品前台
 * 
 * @author cf
 *
 */
@Api(tags = "活动赠品前台")
@RestController
@RequestMapping("/webGiftActive")
public class WebActiveGifgController {
	private static final Logger log = LoggerFactory.getLogger(WebActiveGifgController.class);
	@Autowired
	private AcgoodsService acgoodsService;
	@Autowired
	private AcpgiftService acpgiftService;
	@Autowired
	private StringRedisTemplate redisTemplate;

	@ApiOperation(value = "赠品集合" , response = Acpgift.class)
	@ApiImplicitParams({ @ApiImplicitParam(name = "goodsid", value = "商品id", required = true, paramType = "query") })
	@PostMapping("/acpglist")
	public Object acpglist(Long goodsid) {
		Result result = new Result();
		List<Acpgift> list = null;
		long nowdate = DateUtil.getDateline();
		String types = String.valueOf(AcgoodsType.GiftGOODSACTIVE);
		try {

			if (StringUtils.isEmpty(goodsid)) {
				result.setMsg("商品id不能为空");
				result.setCode(-1);
				return result;
			}
			// 验证是否存在赠品
			Acgoods acgoods = acgoodsService.iniacGoods(goodsid, nowdate, types);
			if (!StringUtils.isEmpty(acgoods)) {
				if (!StringUtils.isEmpty(acgoods.getId())) {

					String acpglist = (String) redisTemplate.opsForValue()
							.get(String.format(ConstantHolder.ACPGLIST, acgoods.getId()));
					// 一般为第一次查询时redis不存在值的情况，查询数据库存到redis缓存中
					if (StringUtils.isEmpty(acpglist)) {
						list = acpgiftService.selectList(new EntityWrapper<Acpgift>().eq("acgoodsid", acgoods.getId())
								.eq("state", 1).eq("type", AcgoodsType.GiftGOODSACTIVE));
						// 有数据，缓存
						if (!list.isEmpty() && list.size() > 0) {
							String key = String.format(ConstantHolder.ACPGLIST, acgoods.getId());
							String value = JSON.toJSON(list).toString();
							try {
								redisTemplate.opsForValue().set(key, value);
							} catch (Exception e) {
								log.error("redis缓存赠品失败，请手动更新缓存，或者联系技术人员");
								result.setCode(-1);
								result.setMsg("redis缓存赠品失败");
								return result;
							}
						}
						result.setData(list);
						result.setResultCode(ResultCode.SUCCESS);
						return result;
					}

					list = JSONObject.parseArray(acpglist, Acpgift.class);
					result.setData(list);
					result.setResultCode(ResultCode.SUCCESS);
					return result;
				}
			}
			return Result.errorMsg("无赠品");

		} catch (Exception e) {
			return GetException.getCatch(e,log);
		}
	}

}
